//
//  StreamViewController.m
//  KoreaFlower
//
//  Created by Nicejinux on 12. 11. 11..
//  Copyright (c) 2012년 Nicejinux. All rights reserved.
//

#import "StreamViewController.h"

//static int MaxPage = 1;

@interface StreamViewController ()

@end

@implementation StreamViewController

@synthesize stream;
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    randomHeights = [[NSMutableArray alloc] initWithCapacity:100];
    for (int i = 0; i < 60; i++) {
        CGFloat h = arc4random() % 200 + 50.f;
        [randomHeights addObject:[NSNumber numberWithFloat:h]];
    }
    stream.scrollsToTop = YES;
    
    stream.cellPadding = 5.0f;
    stream.columnPadding = 5.0f;
    
    [stream reloadData];
    stream.bounces = YES;
}

- (void)viewDidUnload
{
    [self setStream:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSInteger)numberOfCellsInStreamView:(EKStreamView *)streamView
{
    return [randomHeights count];
}

- (NSInteger)numberOfColumnsInStreamView:(EKStreamView *)streamView
{
    return 3;
}

- (UIView *)streamView:(EKStreamView *)streamView cellAtIndex:(NSInteger)index
{
    static NSString *CellID1 = @"MyCell1";
    static NSString *CellID2 = @"MyCell2";
    
    BOOL redCell = index % 3 == 0;
    NSString *CellID =  redCell ? CellID2 : CellID1;
    
    StreamViewCell *cell;
    
    cell = (StreamViewCell *)[streamView dequeueReusableCellWithIdentifier:CellID];
    
    if (cell == nil) {
        cell = [[StreamViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        cell.reuseIdentifier = CellID;
        if (redCell) cell.label.textColor = [UIColor redColor];
        cell.delegate = self;
    }
    
    cell.label.text = [NSString stringWithFormat:@"%d",index];
    
    return cell;
}

- (CGFloat)streamView:(EKStreamView *)streamView heightForCellAtIndex:(NSInteger)index
{
    return [[randomHeights objectAtIndex:index] floatValue];
}

//- (UIView *)headerForStreamView:(EKStreamView *)streamView
//{
//    StreamViewCell *header = [[StreamViewCell alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - stream.columnPadding * 2, 60)];
//    header.label.text = @"This is the header";
//    
//    return header;
//}
//
//- (UIView *)footerForStreamView:(EKStreamView *)streamView
//{
//    if (page <= MaxPage) {
//        StreamViewCell *footer = [[StreamViewCell alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - stream.columnPadding * 2, 60)];
//        footer.label.text = @"This is the footer";
//        
//        return footer;
//    } else {
//        return nil;
//    }
//}

- (void) streamViewDidSelected:(int)index
{
    NSLog(@"selected index : %d", index);
    if ([self.delegate respondsToSelector:@selector(streamViewDidSelected:)] ) {
        [delegate streamViewDidSelected:index];
    }
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (ABS(scrollView.contentSize.height - scrollView.frame.size.height - scrollView.contentOffset.y) < 3
//        && page <= MaxPage) {
//        for (int i = 0; i < 100; i++) {
//            CGFloat h = arc4random() % 200 + 50.f;
//            [randomHeights addObject:[NSNumber numberWithFloat:h]];
//        }
//
//        page++;
//
//        [stream reloadData];
//    }
//}
//

@end
